import { Op } from "sequelize";
import question from "../model/question.js";
import sequelize from "../db/index.js";

export async function addQuestion(questionInfo){
  return await question.create(questionInfo)
}

export async function queryQuestionById(question_id){
  return await question.findOne({
    where:{
      question_id
    }
  })
}

export async function recommendQuestions(tag){
  return await sequelize.query(`SELECT * FROM question WHERE question_tags LIKE "%${tag}%" ORDER BY RAND() LIMIT 10`)
}

export async function queryQuestionByTag(tag){
  return await question.findAll({
    where:{
      question_tags: {
        [Op.like]: `%${tag}%`
      }
    },
    limit: 10
  })
}

export async function goodQuestionReputation(question_id, value){
  return await question.increment({ good_reputation: value}, {
    where: {
      question_id
    }
  })
}

export async function badQuestionReputation(question_id, value){
  return await question.increment({bad_reputation: value}, {
    where: {
      question_id
    }
  })
}

export async function queryQuestionByDate(date){
  return await question.findAll({
    where:{
      create_time:{
        [Op.like]: `%${date}%`
      }
    }
  })
}

export async function queryQuestionByDateline(user_id, dateline){
  return await question.findAll({
    where:{
      question_creator: user_id,
      create_time:{
        [Op.gt]: dateline[0],
        [Op.lte]: dateline[1]
      }
    }
  })
}

export async function queryTagsByUser(user_id){
  return await question.findAll({
    attributes: ['question_tags'],
    where: {
      question_creator: user_id
    }
  })
}

export async function queryQuestionByUser(user_id){
  return await question.findAll({
    where: {
      question_creator: user_id
    },
  })
}

export async function queryQuesitonByUsertag(user_id, tag){
  return await question.findAll({
    where: {
      question_creator: user_id,
      question_tags: tag
    }
  })
}

export async function questionViewCountUp(question_id){
  question.increment({view_count: 1}, {
    where: {
      question_id
    }
  })
}

export async function queryQuestionByKeyword(keyword){
  return await question.findAll({
    where: {
      question_title: {
        [Op.like]: `%${keyword}%`
      }
    }
  })
}

export async function queryHotQuestionByTag(tag){
  return await question.findAll({
    where:{
      question_tags: {
        [Op.like]: `%${tag}%`
      }
    },
    order: [
      ['good_reputation', 'DESC']
    ],
    limit: 10
  })
}

export async function searchIdea(keyword){
  return await question.findAll({
    where: {
      question_title: {
        [Op.like]: `%${keyword}%`
      }
    },
    limit: 10
  })
}

export async function editQuestion(questionInfo){
  return await question.update({
    question_title: questionInfo.question_title,
    question_detail: questionInfo.question_detail,
  },{
    where: {
      question_id: questionInfo.question_id
    }
  })
}

export async function deleteQuestion(question_id){
  return await question.destroy({
    where: {
      question_id,
    }
  })
}